package com.zhangtai.modules.service.impl.security;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dao.SecurityBatchDao;
import com.zhangtai.modules.dto.FundsRecordDto;
import com.zhangtai.modules.entity.security.SecurityBatchEntity;
import com.zhangtai.modules.service.security.SecurityBatchService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;


@RestController
public class SecurityBatchServiceImpl extends ServiceImpl<SecurityBatchDao, SecurityBatchEntity> implements SecurityBatchService {

    @Autowired
    private SecurityBatchDao securityBatchDao;

    @Override
    public PageResult<SecurityBatchEntity> queryPage(@RequestParam(value = "unitName", required = false) String unitName,
                                @RequestParam(value = "orgName", required = false) String orgName,
                                @RequestParam(value = "name", required = false) String name,
                                @RequestParam(value = "time", required = false) String time,
                                                     @RequestParam(value = "endTime", required = false) String endTime,
                                                     @RequestParam(value = "type", required = false) Integer type,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<SecurityBatchEntity> page = this.page(
                new Page<SecurityBatchEntity>(pageNum,pageSize),
                new QueryWrapper<SecurityBatchEntity>().like(!StringUtils.isEmpty(unitName),"unit_name",unitName).
                like(!StringUtils.isEmpty(orgName),"org_name",orgName).like(!StringUtils.isEmpty(name),"name",name)
                        .between(!StringUtils.isEmpty(time)&&!StringUtils.isEmpty(endTime),"confirm_time",time,endTime)
                        .eq(!ObjectUtils.isEmpty(type),"type",type)
                        .eq("is_delete",0)
        );

        return new PageResult(page);
    }



    @Override
    public PageResult<SecurityBatchEntity> queryPageAuth(
            @RequestParam(value = "contractIds",required = false) List<Long> contractIds,
            @RequestParam(value = "unitName", required = false) String unitName,
                                                     @RequestParam(value = "orgName", required = false) String orgName,
                                                     @RequestParam(value = "name", required = false) String name,
                                                     @RequestParam(value = "time", required = false) String time,
                                                     @RequestParam(value = "endTime", required = false) String endTime,
                                                     @RequestParam(value = "type", required = false) Integer type,
                                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

//        IPage<SecurityBatchEntity> page = this.page(
//                new Page<SecurityBatchEntity>(pageNum,pageSize),
//                new QueryWrapper<SecurityBatchEntity>().like(!StringUtils.isEmpty(unitName),"unit_name",unitName).
//                        like(!StringUtils.isEmpty(orgName),"org_name",orgName).like(!StringUtils.isEmpty(name),"name",name)
//                        .between(!StringUtils.isEmpty(time)&&!StringUtils.isEmpty(endTime),"confirm_time",time,endTime)
//                        .eq(!ObjectUtils.isEmpty(type),"type",type)
//                        .eq("is_delete",0)
//        );
        Page<SecurityBatchEntity> page = new Page(pageNum,pageSize);
        List<SecurityBatchEntity> entities = securityBatchDao.getBatchListAuth(contractIds,unitName,orgName,name,time,endTime,type,page);
        page.setRecords(entities);
        return new PageResult<SecurityBatchEntity>(page) ;
    }

    @Override
    public SecurityBatchEntity getByIdE(Long id) {
            SecurityBatchEntity one = this.getOne(new QueryWrapper<SecurityBatchEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(SecurityBatchEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(SecurityBatchEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            SecurityBatchEntity entity = new SecurityBatchEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}